package gui;

import graph.Edge;
import graph.Vertex;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.swing.JPanel;

import util.BinaryMap;

import antGraph.AntEdge;
import antGraph.AntGraph;
import antGraph.AntVertex;

public class GraphScreen extends JPanel{
	
	private HashMap<AntVertex,GUINode> nodesList;
	private ArrayList<GUIEdge> edgesList;
	private AntGraph graph;
	
	private int indexInputX = 0; 
	private int indexInputY = 0;
	
	private int indexOutputX = 0;
	private int indexOutputY = 0;

	public GraphScreen() {
		this.setPreferredSize(new Dimension(800, 800));
		this.graph = new AntGraph();
		nodesList = new HashMap<AntVertex, GUINode>();
		edgesList = new ArrayList<GUIEdge>();
	}
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		for(GUIEdge guiEdge: edgesList){
			guiEdge.draw(g, this);
		}
		
		Set<AntVertex> keys = nodesList.keySet();  
        for (Iterator<AntVertex> iterator = keys.iterator(); iterator.hasNext();)  
        {  
            AntVertex key = iterator.next();  
            if(key != null)  
                nodesList.get(key).draw(g, this);  
        }  
	}
	
	public void addGUINode(AntVertex antVertex){
		GUINode guiNode = new GUINode(antVertex);
		nodesList.put(antVertex, guiNode);
	}
	
	public GUINode getGUINode(AntVertex antVertex){
		return nodesList.get(antVertex);
	}
	
	public void removeGUINode(AntVertex antVertex){
		nodesList.remove(antVertex);
	}
	
	public void addGUIEdge(AntEdge edge){
		GUINode node1 = nodesList.get(edge.getNode1());
		GUINode node2 = nodesList.get(edge.getNode2());
		GUIEdge guiEdge = new GUIEdge(node1,node2);
		edgesList.add(guiEdge);
	}
	

	public void fileToGraph(String filePath){
		BinaryMap binaryMap =  new BinaryMap(filePath);
		int [][]map = binaryMap.createBinaryMap();
		for(int i = 0; i < map.length; i++){
			for(int j = 0; j < map[i].length; j++){
				System.out.print(map[i][j]);
			}
			System.out.println();
		}
		
		//adds all vertices on the graph
		for(int i = 0; i<map.length; i++){
			if(i%2 != 0){
				for(int j = 0; j<map[i].length; j++){
					if(j%2 != 0){
							AntVertex antVertex = new AntVertex(i, j);
							graph.addVertex(antVertex);
							if(map[i][j]== 5){
								indexInputX = i;
								indexInputY = j;
							}else if(map[i][j]== 3){
								indexOutputX = i;
								indexOutputY = j;
							}
					}
				}
			}
		}
		
		//adds all edges
		for(int i = 1; i<map.length-1; i++){
			if(i%2 != 0){
				for(int j = 2; j<map[i].length-2; j=j+2){
					if(map[i][j]==0){
						//System.out.println("Horizontal = i: "+ i+ ", j: "+j );
						AntEdge antEdge = new AntEdge(graph.getVertex(i, j-1), graph.getVertex(i, j+1));
						graph.addEdge(antEdge);
					}
				}
			}else{
				for(int j = 1; j<map[i].length-1; j++){
					if(map[i][j] == 0){
						//System.out.println("Vertical = i: "+ i+ ", j: "+j );
						AntEdge antEdge = new AntEdge(graph.getVertex(i-1, j), graph.getVertex(i+1, j));
						graph.addEdge(antEdge);
					}
				}
			}
		}
		
		//draw the graph
		drawGraph();
		paintInputOutput();
		
	}
	
	public void drawGraph() {
		Set<Vertex> keys = graph.getAllVertices();
		for (Iterator<Vertex> iterator = keys.iterator(); iterator.hasNext();) {
			Vertex key = iterator.next();
			addGUINode((AntVertex) (key));
		}

		Set<Edge> keysEdge = graph.getAllEdges();
		System.out.println("Tamanho: " + keysEdge.size());
		for (Iterator<Edge> iterator = keysEdge.iterator(); iterator.hasNext();) {
			Edge keyEdge = iterator.next();
			addGUIEdge((AntEdge) keyEdge);
		}
	}
	
	private void paintInputOutput(){
		AntVertex antVertex = new AntVertex(indexInputY, indexInputX);
		nodesList.get(antVertex).setColor(Color.GREEN);
		
		antVertex = new AntVertex(indexOutputY, indexOutputX);
		nodesList.get(antVertex).setColor(Color.RED);
	}

}


